<?php
namespace app\api\controller;

use app\common\controller\BaseController;
use app\common\service\MembershipService;
use support\Db;

/**
 * 会员管理控制器
 */
class MembershipController extends BaseController
{
    /**
     * 获取用户会员信息
     */
    public function info()
    {
        $userId = request()->user->id ?? 0;
        
        if (!$userId) {
            return json(['code' => 401, 'msg' => '用户未登录']);
        }

        // 获取用户会员信息
        $membership = MembershipService::getUserMembership($userId);
        $limitInfo = MembershipService::getAppLimitInfo($userId);
        
        // 获取所有会员套餐
        $plans = Db::table('gf_membership_plans')
            ->where('is_active', 1)
            ->orderBy('sort_order')
            ->get()
            ->toArray();

        // 获取用户余额信息
        $balance = Db::table('gf_user_balances')
            ->where('user_id', $userId)
            ->first();

        // 获取最近活动日志
        $recentActivities = MembershipService::getRecentActivities($userId, 5);

        $result = [
            'code' => 200,
            'data' => [
                'membership' => $membership,
                'limit_info' => $limitInfo,
                'plans' => $plans,
                'balance' => $balance,
                'recent_activities' => $recentActivities
            ]
        ];

        return json($result);
    }

    /**
     * 获取会员套餐列表
     */
    public function plans()
    {
        $plans = Db::table('gf_membership_plans')
            ->where('is_active', 1)
            ->orderBy('sort_order')
            ->get()
            ->toArray();

        return json(['code' => 200, 'data' => $plans]);
    }

    /**
     * 检查应用创建权限
     */
    public function checkAppPermission()
    {
        $userId = request()->user->id ?? 0;
        
        if (!$userId) {
            return json(['code' => 401, 'msg' => '用户未登录']);
        }

        $permissionCheck = MembershipService::canCreateApp($userId);
        $limitInfo = MembershipService::getAppLimitInfo($userId);

        return json([
            'code' => 200,
            'data' => [
                'can_create' => $permissionCheck['can_create'],
                'reason' => $permissionCheck['reason'],
                'limit_info' => $limitInfo
            ]
        ]);
    }

    /**
     * 获取账单记录
     */
    public function billingRecords()
    {
        $userId = request()->user->id ?? 0;
        $page = request()->get('page', 1);
        $limit = request()->get('limit', 10);
        $billType = request()->get('bill_type', '');
        
        if (!$userId) {
            return json(['code' => 401, 'msg' => '用户未登录']);
        }

        $query = Db::table('gf_billing_records')
            ->where('user_id', $userId)
            ->orderBy('created_at', 'desc');

        if ($billType) {
            $query->where('bill_type', $billType);
        }

        $total = $query->count();
        $records = $query->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();

        return json([
            'code' => 200,
            'data' => [
                'total' => $total,
                'records' => $records,
                'page' => $page,
                'limit' => $limit
            ]
        ]);
    }

    /**
     * 获取用户操作日志
     */
    public function userLogs()
    {
        $userId = request()->user->id ?? 0;
        $page = request()->get('page', 1);
        $limit = request()->get('limit', 10);
        
        if (!$userId) {
            return json(['code' => 401, 'msg' => '用户未登录']);
        }

        $query = Db::table('gf_user_logs')
            ->where('user_id', $userId)
            ->orderBy('created_at', 'desc');

        $total = $query->count();
        $logs = $query->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();

        return json([
            'code' => 200,
            'data' => [
                'total' => $total,
                'logs' => $logs,
                'page' => $page,
                'limit' => $limit
            ]
        ]);
    }
}